﻿// OperatorPropertyGridCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "OperatorPropertyGridCtrl.h"


// COperatorPropertyGridCtrl

IMPLEMENT_DYNAMIC(COperatorPropertyGridCtrl, CMFCPropertyGridCtrl)

COperatorPropertyGridCtrl::COperatorPropertyGridCtrl()
{

}

COperatorPropertyGridCtrl::~COperatorPropertyGridCtrl()
{
}


BEGIN_MESSAGE_MAP(COperatorPropertyGridCtrl, CMFCPropertyGridCtrl)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// COperatorPropertyGridCtrl 消息处理程序



void COperatorPropertyGridCtrl::SetPropListFont()
{
	::DeleteObject(m_fntPropList.Detach());

	LOGFONT lf;
	afxGlobalData.fontRegular.GetLogFont(&lf);

	NONCLIENTMETRICS info;
	info.cbSize = sizeof(info);

	afxGlobalData.GetNonClientMetrics(info);

	lf.lfHeight = info.lfMenuFont.lfHeight;
	lf.lfWeight = info.lfMenuFont.lfWeight;
	lf.lfItalic = info.lfMenuFont.lfItalic;

	m_fntPropList.CreateFontIndirect(&lf);

	this->SetFont(&m_fntPropList);
}


int COperatorPropertyGridCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMFCPropertyGridCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	//SetPropListFont();

	return 0;
}


void COperatorPropertyGridCtrl::Init2()
{
	HDITEM item;
	item.cxy = 200;
	item.mask = HDI_WIDTH;
	GetHeaderCtrl().ShowWindow(SW_HIDE);
	GetHeaderCtrl().SetItem(0, new HDITEM(item));
	SetPropListFont();

}

void COperatorPropertyGridCtrl::Populate(ck::Operator* pOperator)
{
	PopulateInfo(pOperator);

	PopulateInput(pOperator->GetInput());
	PopulateOutput(pOperator);
	PopulateRule(pOperator->GetRule());

	//char param[_MAX_PATH];
	//char value[_MAX_PATH];

	//CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(_T("参数"));
	//memset(param, 0, _MAX_PATH);
	//memset(value, 0, _MAX_PATH);
	//strcpy_s(param, "输入1");
	//strcpy_s(value, "D:\\data\\1.tif");
	//pGroup->AddSubItem(new CMFCPropertyGridProperty(param, (_variant_t)value, _T("")));

	//memset(param, 0, _MAX_PATH);
	//memset(value, 0, _MAX_PATH);
	//strcpy_s(param, "输入2");
	//strcpy_s(value, "D:\\data\\2.tif");
	//pGroup->AddSubItem(new CMFCPropertyGridProperty(param, (_variant_t)value, _T("")));

	//memset(param, 0, _MAX_PATH);
	//memset(value, 0, _MAX_PATH);
	//strcpy_s(param, "步长");
	//strcpy_s(value, "3");
	//pGroup->AddSubItem(new CMFCPropertyGridProperty(param, (_variant_t)value, _T("")));

	//AddProperty(pGroup);
}

void COperatorPropertyGridCtrl::PopulateInfo(ck::Operator* pOperator)
{
	CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(_T("算子信息"));

	const char* szName = pOperator->GetName();
	pGroup->AddSubItem(new CMFCPropertyGridProperty("算子名称", (_variant_t)szName, _T("")));

	const char* szModName = pOperator->GetModuleName();
	pGroup->AddSubItem(new CMFCPropertyGridProperty("所属模块", (_variant_t)szModName, _T("")));

	const char* szDeveloper = pOperator->GetModule()->GetDeveloper();
	pGroup->AddSubItem(new CMFCPropertyGridProperty("开发者", (_variant_t)szDeveloper, _T("")));

	const char* szDescription = pOperator->GetModule()->GetDescription();
	pGroup->AddSubItem(new CMFCPropertyGridProperty("算子说明", (_variant_t)szDescription, _T("")));
	
	AddProperty(pGroup);
}

void COperatorPropertyGridCtrl::PopulateInput(ck::OpInput* pInput)
{
	CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(_T("输入信息"));
	
	ck::Argument* pArgument = NULL;
	int nCount = pInput->GetArgCount();
	for (int i = 0; i < nCount; i++)
	{
		pArgument = pInput->GetArgument(i);	

		AddArgment(pGroup, pArgument);
	}

	AddProperty(pGroup);
}

void COperatorPropertyGridCtrl::PopulateOutput(ck::Operator* pOperator)
{
	CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(_T("输出信息"));

	AddProperty(pGroup);
}

void COperatorPropertyGridCtrl::PopulateRule(ck::OpRule* pRule)
{
	CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(_T("检查规则"));

	ck::Constraint* pConstraint = NULL;
	int nConstraint = pRule->GetCount();
	for (int i = 0; i < nConstraint; i++)
	{
		pConstraint = pRule->GetConstraint(i);
		if (pConstraint)
		{
			AddConstraint(pGroup, pConstraint);
		}
	}	

	AddProperty(pGroup);
}

void COperatorPropertyGridCtrl::AddArgment(CMFCPropertyGridProperty* pGroup, ck::Argument* pArgument)
{
	const char	*szKey = pArgument->GetKey();
	const char	*szAlias = pArgument->GetAlias();
	ck::Value	*pValue = pArgument->GetValue();
	const char	*szType = ck::Value::GetValueTypeString(pValue->GetType());

	pGroup->AddSubItem(new CMFCPropertyGridProperty(szAlias, (_variant_t)szType, _T("")));
}

void COperatorPropertyGridCtrl::AddConstraint(CMFCPropertyGridProperty* pGroup, ck::Constraint* pConstraint)
{
	const char* szKey = pConstraint->GetKey();
	const char* szDesc = pConstraint->GetDescription();
	ck::Value*  pValue = pConstraint->GetValue();

	char szValue[Z_MESSAGE_MAX];
	memset(szValue, 0, Z_MESSAGE_MAX);
	pValue->GetAsString(szValue, Z_MESSAGE_MAX);

	pGroup->AddSubItem(new CMFCPropertyGridProperty(szDesc, (_variant_t)szValue, _T("")));
}